// ************************************************************
// Copyright © 2021
// File Name: bookstore.go
// Author:
// Email:
// Created: 2021-12-27 09:48:13 (CST)
// Last Update:
//          By:
// Description:
// ************************************************************

package biz

import (
	"bookstore/internal/data"
)

type BookDO struct {
	Id      string
	Name    string
	Authors []string
	Press   string
}

type BookBiz struct {
	memStore *data.MemStore
}

func NewBookBiz(store *data.MemStore) *BookBiz {
	return &BookBiz{
		memStore: store,
	}
}

func (bookbiz *BookBiz) Get(id string) (*BookDO, error) {
	b, err := bookbiz.memStore.Get(id)
	if err != nil {
		return nil, err
	}
	return &BookDO{
		Id:      b.Id,
		Name:    b.Name,
		Authors: b.Authors,
		Press:   b.Press,
	}, nil
}

func (bookbiz *BookBiz) Add(book *BookDO) error {
	nBook := new(data.BookPO)
	nBook.Id = book.Id
	nBook.Name = book.Name
	nBook.Authors = book.Authors
	nBook.Press = book.Press
	err := bookbiz.memStore.Add(nBook)
	return err
}
